%valutatore_derivate
evalExpr(Term,Value,R) :- evalTerm(Term,Value,R).
evalExpr(Exp+Term, Value,R) :- evalExpr(Exp,Value,R1),
	evalTerm(Term,Value,R2),R is R1 + R2.

evalExpr(Exp-Term, Value,R) :- evalExpr(Exp,Value,R1),
	evalTerm(Term,Value,R2),R is R1 - R2.

evalTerm(Factor, Value,R) :- evalFactor(Factor, Value,R).
evalTerm(Term*Factor, Value,R) :- evalTerm(Term, Value,R1),
	evalFactor(Factor, Value,R2),R is R1 * R2.
evalTerm(Term/Factor, Value,R) :- evalTerm(Term, Value,R1),
	evalFactor(Factor, Value,R2), R is R1 / R2.

evalFactor(x,V,V).
evalFactor(N,_,N):-number(N).
evalFactor([Exp], Value,R) :- evalExpr(Exp, Value,R).
evalFactor(-Exp,Value,R):- evalExpr(Exp, Value,R1),R is -R1.
evalFactor(sin(Exp),Value,R):-evalExpr(Exp,Value,R1),R is sin(R1).
evalFactor(cos(Exp),Value,R):-evalExpr(Exp,Value,R1),R is cos(R1).